﻿// -----------------------------------------------------------------------
// <copyright file="TimeSpanExtensionMethods.cs" company="Microsoft">
//  Srsly?
// </copyright>
// -----------------------------------------------------------------------

namespace Eeeek.ExtensionMethods
{
    using System;
    using System.Globalization;

    /// <summary>
    /// Methods to extend the .NET TimeSpan class.
    /// </summary>
    public static class TimeSpanExtensionMethods
    {
        /// <summary>
        /// Creates a formatted string.
        /// </summary>
        /// <param name="span">The time span.</param>
        /// <returns>A string representation of the timespan</returns>
        public static string ToFormattedString(this TimeSpan span)
        {
            int hours = span.Hours;
            span = span.Subtract(new TimeSpan(0, hours, 0, 0));

            int minutes = span.Minutes;
            span = span.Subtract(new TimeSpan(0, 0, minutes, 0));

            int seconds = span.Seconds;
            span = span.Subtract(new TimeSpan(0, 0, 0, seconds));

            int milliSeconds = span.Milliseconds;

            return string.Format(
                "{0}:{1}:{2}.{3}", 
                hours.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0'), 
                minutes.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0'), 
                seconds.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0'), 
                milliSeconds.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0'));
        }
    }
}
